package com.ruoyi.psi.inventory.check.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.inventory.check.domain.InventoryCheck;
import com.ruoyi.psi.inventory.check.service.IInventoryCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存盘点
 */
@RestController
@RequestMapping("/psi/inventory/check")
@Api(value = "库存盘点模块", tags = "库存盘点接口")
public class InventoryCheckController extends BaseController {
    @Autowired
    private IInventoryCheckService inventoryCheckService;


    /**
     * 获取仓库盘点
     * @param inventoryCheck
     * @return
     */
    @ApiOperationSupport(includeParameters = {"PurchaseApply.inventoryCheckId", "PurchaseApply.staffId", "PurchaseApply.checkTime"})
    @ApiOperation(value = "获取仓库盘点列表",notes = "查询所有仓库盘点信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody InventoryCheck inventoryCheck) {
        startPage();
        List<InventoryCheck> inventoryChecks = inventoryCheckService.selectInventoryCheckList(inventoryCheck);
        return getDataTable(inventoryChecks);
    }

    /**
     * 导出仓库盘点
     *
     * @param inventoryCheck
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody InventoryCheck inventoryCheck) {
        List<InventoryCheck> inventoryChecks = inventoryCheckService.selectInventoryCheckList(inventoryCheck);
        ExcelUtil<InventoryCheck> util = new ExcelUtil<InventoryCheck>(InventoryCheck.class);
        return util.exportExcel(inventoryChecks, "仓库盘点单数据");
    }

    /**
     * 新增仓库盘点
     * @param inventoryCheck 仓库盘点单据
     * @return
     */
    @ApiOperation(value = "新增仓库盘点", notes = "新增仓库盘点")
    @ApiImplicitParam(name = "inventoryCheck", value = "新增仓库盘点", dataType = "InventoryCheck")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody InventoryCheck inventoryCheck) {
        try {
            inventoryCheckService.insertInventoryCheck(inventoryCheck);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新仓库盘点
     * @param inventoryCheck 仓库盘点单据
     * @return
     */
    @ApiOperation(value = "更新仓库盘点", notes = "更新仓库盘点")
    @ApiImplicitParam(name = "inventoryCheck", value = "更新仓库盘点", dataType = "InventoryCheck")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody InventoryCheck inventoryCheck) {
        try {
            inventoryCheckService.updateInventoryCheck(inventoryCheck);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除仓库盘点
     * 仅修改删除标志位
     *
     * @param inventoryCheck 仓库盘点单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody InventoryCheck inventoryCheck) {

        return toAjax(inventoryCheckService.updateInventoryCheckDeleteFlag(inventoryCheck));
    }




}
